const got = require("got")
var token = require("./token.js");
const config = require("./config.js");
const { json } = require("express");

class DB {
	constructor() {
		this.env = config.env;
		this.token = token
	}

	/**
	 * 添加记录
	 * @params  collection  集合名称								字符串
	 * 			data		[{realname:"张三"},{realname:"李四"}]   对象数组
	 * 
	*/

	async add({ collection, data }) {
		var token = await this.token
		data = JSON.stringify(data)
		var query = `db.collection("${collection}").add({data:${data}})`

		var context = {
			"env": this.env,
			"query": query
		}
		var url = "https://api.weixin.qq.com/tcb/databaseadd?access_token=" + token;
		// return await got.post(url, { json: context })
		let result = await got.post(url, { json: context })
		return JSON.parse(result.body)
	}


	/**
	 * 
	 * 读取记录
	*/
	async get({ collection, where = {}, limit = 100, skip = 1 }) {
		where = JSON.stringify(where)
		var token = await this.token
		var query = `db.collection("${collection}").where(${where}).get()`
		// console.log(query);return;

		var context = {
			"env": this.env,
			"query": query
		}
		// console.log(context, "1111");
		var url = "https://api.weixin.qq.com/tcb/databasequery?access_token=" + token;
		let result = await got.post(url, { json: context })
		return JSON.parse(result.body)
	}


	/**
	 * 删除记录
	 * @params   collection   集合名称
	 * 			where         查询条件  {}
	 * 
	*/
	async remove({ collection = "", where = {} }) {
		collection = JSON.stringify(collection)
		where = JSON.stringify(where)
		var token = await this.token
		var query = `db.collection(${collection}).where(${where}).remove()`
		var context = {
			"env": this.env,
			"query": query
		}
		var url = "https://api.weixin.qq.com/tcb/databaseadd?access_token=" + token;
		let result = await got.post(url, { json: context })
		return JSON.parse(result.body)
	}


	/**
	 * 删除一条记录
	 * @params   collection   集合名称
	 * 			id             记录id  
	 * 
	*/
	async removeById({ collection = "", id = "" }) {
		var token = await this.token
		var query = `db.collection("${collection}").doc("${id}").remove()`
		var context = {
			"env": this.env,
			"query": query
		}
		var url = "https://api.weixin.qq.com/tcb/databasedelete?access_token=" + token;
		let result = await got.post(url, { json: context })
		return JSON.parse(result.body)
	}

	/**
	 * 更新记录
	 * @params   collection   集合名称
	 * 			where         查询条件  {}
	 * 
	*/
	async update({ collection = "", where = {}, data = {} }) {
		where = JSON.stringify(where)
		data = JSON.stringify(data)
		var token = await this.token
		var query = `db.collection("${collection}").where(${where}).update({data:${data}})`
		var context = {
			"env": this.env,
			"query": query
		}
		console.log(context);
		var url = "https://api.weixin.qq.com/tcb/databaseupdate?access_token=" + token;
		let result = await got.post(url, { json: context })
		return JSON.parse(result.body)
	}


	/**
	 * 更新一条记录
	 * @params   collection   集合名称
	 * 			where         查询条件  {}
	 * 
	*/
	async updateById({ collection = "", id = "", data = {} }) {
		collection = JSON.stringify(collection)
		data = JSON.stringify({ data: data })
		id = JSON.stringify(id)

		var token = await this.token
		var query = `db.collection(${collection}).doc(${id}).databaseupdate({data:{realname:"张三丰"}})`
		var context = {
			"env": this.env,
			"query": query
		}
		console.log(context);
		var url = "https://api.weixin.qq.com/tcb/databaseadd?access_token=" + token;
		let result = await got.post(url, { json: context })
		console.log(result.body, "2222"); return;
		return JSON.parse(result.body)
	}


}

module.exports = new DB()
